package main

import (
	"fmt"
	"sync"
)

/**
实现一个带有缓冲的通道，生产者协程向通道中发送100个整数，消费者协程从通道中接收这些整数并打印。
*/

var wg sync.WaitGroup

func producer(c chan int) {
	for i := 0; i < 100; i++ {
		c <- i
		fmt.Println("producer: ", i)
	}
	defer wg.Done()
	defer close(c)

}

func consumer(c chan int) {
	for v := range c {
		fmt.Println("consumer", v)
	}
	defer wg.Done()
}

func main() {
	var c = make(chan int, 1)
	wg.Add(1)
	go producer(c)
	wg.Add(1)
	go consumer(c)

	wg.Wait()
}
